package com.zpark.controller;

import com.zpark.entity.Review;
import com.zpark.service.IReviewService;
import com.zpark.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("reviews")
public class ReviewController extends BaseController {

    @Autowired
    private IReviewService reviewService;

    @RequestMapping("addreview/{pid}/{name}/{review}/{email}")
    public JsonResult<Void> addReview(@PathVariable("pid") Integer pid, @PathVariable("name") String name, @PathVariable("email") String email, @PathVariable("review") String sreview, HttpSession session){
        Review review = new Review();
        review.setPid(pid);
        review.setUid(getUidFromSession(session));
        review.setUsername(getUsernameFromSession(session));
        review.setCreatedUser(getUsernameFromSession(session));
        review.setCreatedTime(new Date());
        review.setCreatedTime(new Date());
        review.setName(name);
        review.setReview(sreview);
        review.setEmail(email);
        reviewService.insertReview(pid, name, email, sreview, review);
        return new JsonResult<Void>(OK);
    }

    @RequestMapping("findReview/{pid}")
    public JsonResult<List<Review>> findReviewByPid(@PathVariable("pid") Integer pid){
        List<Review> data = reviewService.findReviewByPid(pid);
        return new JsonResult<List<Review>>(OK, data);
    }

    @RequestMapping("delete/{pid}/{rid}")
    public JsonResult<Void> deleteReviewByPidAndRid(@PathVariable("pid") Integer pid, @PathVariable("rid") Integer rid){
        reviewService.deleteReviewByPidAndRid(pid, rid);
        return new JsonResult<Void>(OK);
    }

}
